﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace OpenXW
{
    /// <summary>
    /// A representation of an XtremeWorlds item.
    /// </summary>
    public class Item
    {
        private string _Name;
        private string _Description;
        private short _Sprite;
        private int _MinDamage;
        private int _MaxDamage;

        public ItemType _ItemType;

        public String Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        public String Description
        {
            get { return _Description; }
            set { _Description = value; }
        }

        public int Min_Damage
        {
            get { return _MinDamage; }
        }

        public int Max_Damage
        {
            get { return _MaxDamage; }
        }

        public short Sprite
        {
            get { return _Sprite; }
        }

        /// <summary>
        /// Creates the item from a file. 
        /// </summary>
        /// <param name="fileName">The filename of the item to load.</param>
        /// <returns></returns>
        public static Item FromFile(string fileName)
        {
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            Item _Item = new Item();
            FileStream fs = new FileStream(@fileName, FileMode.Open);
            BinaryReader bReader = new BinaryReader(fs);

            //What do the first two bytes do? I'm not sure... needs confirmation... let's just
            //skip over that for now.
            bReader.BaseStream.Position = 2;

            //OFFSET 2: The next 25 bytes are the the item name! ReadString dosen't work here due to VB6s formatting.
            _Item.Name = encoding.GetString(bReader.ReadBytes(25));

            //OFFSET 27:
            

            //OFFSET 59: The sprite of the picture.
            bReader.BaseStream.Position = 59;
            _Item._Sprite = bReader.ReadInt16();

            //OFFSET 61: The ItemType is stored as a byte is here!
            bReader.BaseStream.Position = 61;
            _Item._ItemType = (ItemType)bReader.ReadByte();

            //OFFSET 66: Read the MinDamage, 4 bytes VB6 long.
            bReader.BaseStream.Position = 66;
            _Item._MinDamage = bReader.ReadInt32();

            //OFFSET 70:

            //OFFSET 72: 100 bytes for the description...
            bReader.BaseStream.Position = 72;
            _Item.Description = encoding.GetString(bReader.ReadBytes(100));

            //OFFSET 234: Read the MaxDamage, 4 bytes VB6 long.
            bReader.BaseStream.Position = 234;
            _Item._MaxDamage = bReader.ReadInt32();



            bReader.Close();
            return _Item;

        }

        public enum ItemType : byte
        {
            Weapon = 1,
            ARMOR = 2,
            Helmet = 3,
            Shield = 4,
            PotionAddHP = 5,
            POTIONADDMP = 6,
            POTIONADDSP = 7,
            POTIONSUBHP = 8,
            POTIONSUBMP = 9,
            POTIONSUBSP = 10,
            KEY = 11,
            Currency = 12,
            SPELL = 13,
            UNKNOWN_TYPE1 = 14,
            UNKNOWN_TYPE2 = 15,
            UNKNOWN_TYPE3 = 16,
            BOOK = 17
        }




    }

}
